/**
 * 
 */
package at.ac.tuwien.swa075.telco.hlr.dao.hibernate;

import java.util.List;

import org.hibernate.Transaction;

import at.ac.tuwien.swa075.telco.hlr.HlrException;
import at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO;
import at.ac.tuwien.swa075.telco.hlr.dao.entity.SubscriptionEntity;

/**
 * @author vovmj01
 *
 */
public class HibernateSubscriberDAO implements ISubscriberDAO {

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#deleteSubscriber(int)
	 */
	public boolean deleteSubscriber(int subscriberId) throws HlrException {
		SubscriptionEntity sub = findSubscription(subscriberId);
		if (sub == null) {
			return false;
		}
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().delete(sub);
		transcation.commit();
		return true;
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#findSubscriptionByEquipment(java.lang.String)
	 */
	public SubscriptionEntity findSubscriptionByEquipment(String imei) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		SubscriptionEntity sub = (SubscriptionEntity) HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM SubscriptionEntity as h WHERE imei = '" + imei + "'").uniqueResult();
		transcation.commit();
		return sub;
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#findSubscriptionByImsi(java.lang.String)
	 */
	public SubscriptionEntity findSubscriptionByImsi(String imsi) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		SubscriptionEntity sub = (SubscriptionEntity) HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM SubscriptionEntity as h WHERE imsi = '" + imsi + "'").uniqueResult();
		transcation.commit();
		return sub;
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#findSubscriptionByPhoneNumber(java.lang.String, java.lang.String)
	 */
	public SubscriptionEntity findSubscriptionByPhoneNumber(String code, String number) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		SubscriptionEntity sub = (SubscriptionEntity) HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM SubscriptionEntity as h WHERE code = '" + code + "' and number = '" + number + "'").uniqueResult();
		transcation.commit();
		return sub;
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#insertSubscription(at.ac.tuwien.swa075.telco.hlr.dao.entities.SubscriptionEntity)
	 */
	public int insertSubscription(SubscriptionEntity subscription) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().save(subscription);
		transcation.commit();
		return subscription.getId();
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#selectSubscriptionsByStatus(java.lang.String)
	 */
	public List<SubscriptionEntity> selectSubscriptionsByStatus(String status) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		// FIXME: status
		List subs = HibernateSessionFactory.getInstance().getCurrentSession().createQuery("SELECT h FROM SubscriptionEntity as h WHERE status = " + status).list();
		transcation.commit();
		return subs;
	}

	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.hlr.dao.ISubscriberDAO#updateSubscription(at.ac.tuwien.swa075.telco.hlr.dao.entities.SubscriptionEntity)
	 */
	public boolean updateSubscription(SubscriptionEntity subscription) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		HibernateSessionFactory.getInstance().getCurrentSession().update(subscription);
		transcation.commit();
		return true;
	}

	public SubscriptionEntity findSubscription(int id) throws HlrException {
		Transaction transcation = HibernateSessionFactory.getInstance().getCurrentSession().beginTransaction();
		SubscriptionEntity sub = (SubscriptionEntity) HibernateSessionFactory.getInstance().getCurrentSession().get(SubscriptionEntity.class, Integer.valueOf(id));
		transcation.commit();
		return sub;
	}

}
